{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import socket\n",
    "from bs4 import BeautifulSoup\n",
    "headers = {\n",
    "    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',\n",
    "    'Accept-Language':'zh-CN,zh;q=0.8',\n",
    "    'Connection':'keep-alive',\n",
    "    'Content-Type': 'application/x-www-form-urlencoded',\n",
    "    'User-Agent':\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 \"\n",
    "                  \"Safari/537.36 SE 2.X MetaSr 1.0\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "171.44.74.157:61234\n",
      "114.236.97.181:61234\n",
      "223.241.117.246:18118\n",
      "223.241.118.230:18118\n",
      "117.36.103.170:8118\n",
      "223.241.78.22:18118\n",
      "218.72.67.118:18118\n",
      "183.159.82.48:18118\n",
      "60.177.229.175:18118\n",
      "101.27.21.32:61234\n",
      "183.4.30.232:8118\n",
      "218.72.67.33:18118\n",
      "115.203.192.55:49138\n",
      "223.241.116.200:8010\n",
      "60.177.231.72:18118\n",
      "223.241.78.89:8010\n",
      "114.100.183.89:61234\n",
      "183.159.88.132:18118\n",
      "60.177.224.242:18118\n",
      "60.177.225.105:18118\n",
      "49.83.132.188:61234\n",
      "183.159.83.11:18118\n",
      "119.186.232.168:61234\n",
      "111.170.82.147:61234\n",
      "180.109.81.194:8118\n",
      "183.128.33.155:18118\n",
      "60.177.226.250:18118\n",
      "183.159.93.177:18118\n",
      "183.159.93.38:18118\n",
      "60.189.112.9:61234\n",
      "183.159.95.70:18118\n",
      "60.173.242.141:808\n",
      "119.28.194.66:8888\n",
      "122.114.31.177:808\n",
      "61.135.217.7:80\n",
      "180.109.139.19:8118\n",
      "218.72.110.106:18118\n",
      "139.227.252.141:8118\n",
      "183.159.92.93:18118\n",
      "218.72.108.163:18118\n",
      "183.159.94.157:18118\n",
      "218.72.67.189:18118\n",
      "113.124.217.123:61234\n",
      "60.177.224.121:18118\n",
      "183.128.32.18:18118\n",
      "183.159.94.230:18118\n",
      "49.83.247.142:61234\n",
      "60.177.227.107:18118\n",
      "49.79.194.4:61234\n",
      "182.202.220.44:61234\n",
      "183.159.82.121:18118\n",
      "60.177.225.139:18118\n",
      "223.241.119.121:8010\n",
      "218.72.108.37:18118\n",
      "117.71.148.5:61234\n",
      "218.72.110.164:18118\n",
      "183.159.86.145:18118\n",
      "123.12.66.231:61234\n",
      "223.245.176.158:61234\n",
      "123.56.89.238:60443\n",
      "115.53.59.148:61234\n",
      "183.159.95.132:18118\n",
      "183.159.86.38:18118\n",
      "49.79.194.214:61234\n",
      "114.239.121.166:61234\n",
      "125.127.158.162:61234\n",
      "114.233.98.69:61234\n",
      "223.241.78.178:18118\n",
      "60.177.229.244:18118\n",
      "221.228.17.172:8181\n",
      "60.189.115.176:61234\n",
      "183.159.81.189:18118\n",
      "14.120.183.36:61234\n",
      "60.177.226.200:18118\n",
      "123.161.119.223:61234\n",
      "221.224.49.237:3128\n",
      "113.86.220.226:808\n",
      "60.177.231.0:18118\n",
      "171.13.82.245:61234\n",
      "114.249.71.145:8118\n",
      "183.159.94.91:18118\n",
      "114.236.37.251:61234\n",
      "42.7.26.21:60443\n",
      "123.55.191.146:49480\n",
      "183.128.34.165:18118\n",
      "180.212.140.230:8118\n",
      "49.79.196.239:61234\n",
      "218.72.108.188:18118\n",
      "117.93.19.117:61234\n",
      "180.110.249.210:8118\n",
      "123.161.119.60:61234\n",
      "183.128.35.77:18118\n",
      "183.159.88.47:18118\n",
      "121.61.88.56:61234\n",
      "49.83.173.127:61234\n",
      "114.239.90.210:61234\n",
      "183.159.91.139:18118\n",
      "183.159.93.87:18118\n",
      "180.160.116.205:8118\n",
      "111.180.198.182:61234\n"
     ]
    }
   ],
   "source": [
    "# def get_ip():\n",
    "proxy = []\n",
    "url_ip = 'http://www.xicidaili.com/nn/'\n",
    "response = requests.get(url_ip, headers=headers)\n",
    "soup = BeautifulSoup(response.text,'lxml')\n",
    "ips =  soup.find_all('tr')\n",
    "for i in range(1,len(ips)):\n",
    "    ip_conntent = ips[i]\n",
    "    a = ip_conntent.find_all('td')\n",
    "    ip_port = a[1].get_text()+':'+a[2].get_text()\n",
    "    print(ip_port)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "183.128.34.165:18118\n",
      "180.109.139.19:8118\n",
      "183.128.35.77:18118\n",
      "183.159.82.121:18118\n",
      "60.177.229.244:18118\n",
      "49.83.173.127:61234\n",
      "218.72.110.106:18118\n",
      "114.239.90.210:61234\n",
      "223.241.78.178:18118\n",
      "183.159.91.139:18118\n",
      "183.159.93.87:18118\n",
      "183.159.93.177:18118\n",
      "180.160.116.205:8118\n",
      "111.180.198.182:61234\n",
      "14.120.181.71:61234\n",
      "122.4.29.213:61234\n",
      "114.233.98.69:61234\n",
      "60.177.226.250:18118\n",
      "183.128.33.155:18118\n",
      "125.127.158.162:61234\n",
      "60.177.227.245:18118\n",
      "180.109.81.194:8118\n",
      "121.61.88.56:61234\n",
      "183.159.88.47:18118\n",
      "114.239.121.166:61234\n",
      "60.177.224.144:18118\n",
      "61.135.217.7:80\n",
      "122.114.31.177:808\n",
      "60.173.242.141:808\n",
      "111.170.82.147:61234\n",
      "119.28.194.66:8888\n",
      "183.159.95.196:18118\n",
      "49.79.196.239:61234\n",
      "180.110.249.210:8118\n",
      "119.186.232.168:61234\n",
      "117.93.19.117:61234\n",
      "49.79.196.51:61234\n",
      "183.159.83.11:18118\n",
      "60.177.225.105:18118\n",
      "223.241.119.22:8010\n",
      "49.79.194.214:61234\n",
      "218.72.66.99:18118\n",
      "218.72.66.134:18118\n",
      "183.159.95.132:18118\n",
      "223.241.119.187:18118\n",
      "218.72.108.188:18118\n",
      "218.72.110.100:18118\n",
      "223.241.119.108:8010\n",
      "49.79.196.157:61234\n",
      "60.177.228.244:18118\n",
      "114.100.188.69:61234\n",
      "58.251.208.120:8118\n",
      "49.83.247.145:61234\n",
      "115.53.59.148:61234\n",
      "183.159.92.93:18118\n",
      "49.79.192.75:61234\n",
      "60.177.227.174:18118\n",
      "60.177.228.44:18118\n",
      "123.56.89.238:60443\n",
      "180.212.140.230:8118\n",
      "223.145.229.161:6666\n",
      "171.37.164.64:8123\n",
      "60.177.224.242:18118\n",
      "223.241.116.37:8010\n",
      "218.72.108.224:18118\n",
      "223.241.118.142:8010\n",
      "114.99.29.177:18118\n",
      "223.245.176.158:61234\n",
      "125.120.201.2:6666\n",
      "123.12.66.231:61234\n",
      "183.159.80.63:18118\n",
      "221.228.17.172:8181\n",
      "42.7.26.21:60443\n",
      "183.128.32.90:18118\n",
      "223.241.118.155:18118\n",
      "183.159.94.157:18118\n",
      "183.159.86.15:18118\n",
      "183.159.83.202:18118\n",
      "118.122.92.252:37901\n",
      "49.79.193.62:61234\n",
      "221.5.54.6:808\n",
      "49.79.195.126:61234\n",
      "218.72.67.189:18118\n",
      "223.241.117.19:18118\n",
      "218.72.108.68:18118\n",
      "218.72.67.33:18118\n",
      "183.159.86.145:18118\n",
      "183.4.30.232:8118\n",
      "183.159.90.62:18118\n",
      "110.73.40.102:8123\n",
      "117.68.194.43:18118\n",
      "182.122.164.141:61234\n",
      "223.241.78.220:18118\n",
      "49.79.194.189:61234\n",
      "101.27.21.32:61234\n",
      "218.72.111.195:18118\n",
      "183.159.87.82:18118\n",
      "114.236.37.251:61234\n",
      "218.72.66.76:18118\n",
      "183.159.87.161:18118\n",
      "检测183.128.34.165:18118是否有效...\n",
      "检测180.109.139.19:8118是否有效...\n",
      "180.109.139.19:8118有效...\n",
      "检测183.128.35.77:18118是否有效...\n",
      "检测183.159.82.121:18118是否有效...\n",
      "183.159.82.121:18118有效...\n",
      "检测60.177.229.244:18118是否有效...\n",
      "检测49.83.173.127:61234是否有效...\n",
      "检测218.72.110.106:18118是否有效...\n",
      "检测114.239.90.210:61234是否有效...\n",
      "检测223.241.78.178:18118是否有效...\n",
      "检测183.159.91.139:18118是否有效...\n",
      "183.159.91.139:18118有效...\n",
      "检测183.159.93.87:18118是否有效...\n",
      "检测183.159.93.177:18118是否有效...\n",
      "检测180.160.116.205:8118是否有效...\n",
      "检测111.180.198.182:61234是否有效...\n",
      "检测14.120.181.71:61234是否有效...\n",
      "14.120.181.71:61234有效...\n",
      "检测122.4.29.213:61234是否有效...\n",
      "122.4.29.213:61234有效...\n",
      "检测114.233.98.69:61234是否有效...\n",
      "检测60.177.226.250:18118是否有效...\n",
      "检测183.128.33.155:18118是否有效...\n",
      "检测125.127.158.162:61234是否有效...\n",
      "检测60.177.227.245:18118是否有效...\n",
      "检测180.109.81.194:8118是否有效...\n",
      "检测121.61.88.56:61234是否有效...\n",
      "检测183.159.88.47:18118是否有效...\n",
      "检测114.239.121.166:61234是否有效...\n",
      "检测60.177.224.144:18118是否有效...\n",
      "检测61.135.217.7:80是否有效...\n",
      "61.135.217.7:80有效...\n",
      "检测122.114.31.177:808是否有效...\n",
      "122.114.31.177:808有效...\n",
      "检测60.173.242.141:808是否有效...\n",
      "60.173.242.141:808有效...\n",
      "检测111.170.82.147:61234是否有效...\n",
      "检测119.28.194.66:8888是否有效...\n",
      "119.28.194.66:8888有效...\n",
      "检测183.159.95.196:18118是否有效...\n",
      "183.159.95.196:18118有效...\n",
      "检测49.79.196.239:61234是否有效...\n",
      "检测180.110.249.210:8118是否有效...\n",
      "检测119.186.232.168:61234是否有效...\n",
      "检测117.93.19.117:61234是否有效...\n",
      "检测49.79.196.51:61234是否有效...\n",
      "检测183.159.83.11:18118是否有效...\n",
      "检测60.177.225.105:18118是否有效...\n",
      "检测223.241.119.22:8010是否有效...\n",
      "检测49.79.194.214:61234是否有效...\n",
      "检测218.72.66.99:18118是否有效...\n",
      "检测218.72.66.134:18118是否有效...\n",
      "检测183.159.95.132:18118是否有效...\n",
      "183.159.95.132:18118有效...\n",
      "检测223.241.119.187:18118是否有效...\n",
      "检测218.72.108.188:18118是否有效...\n",
      "检测218.72.110.100:18118是否有效...\n",
      "检测223.241.119.108:8010是否有效...\n",
      "检测49.79.196.157:61234是否有效...\n",
      "检测60.177.228.244:18118是否有效...\n",
      "检测114.100.188.69:61234是否有效...\n",
      "检测58.251.208.120:8118是否有效...\n",
      "检测49.83.247.145:61234是否有效...\n",
      "检测115.53.59.148:61234是否有效...\n",
      "检测183.159.92.93:18118是否有效...\n",
      "检测49.79.192.75:61234是否有效...\n",
      "49.79.192.75:61234有效...\n",
      "检测60.177.227.174:18118是否有效...\n",
      "60.177.227.174:18118有效...\n",
      "检测60.177.228.44:18118是否有效...\n",
      "检测123.56.89.238:60443是否有效...\n",
      "123.56.89.238:60443有效...\n",
      "检测180.212.140.230:8118是否有效...\n",
      "检测223.145.229.161:6666是否有效...\n",
      "检测171.37.164.64:8123是否有效...\n",
      "171.37.164.64:8123有效...\n",
      "检测60.177.224.242:18118是否有效...\n",
      "检测223.241.116.37:8010是否有效...\n",
      "检测218.72.108.224:18118是否有效...\n",
      "检测223.241.118.142:8010是否有效...\n",
      "检测114.99.29.177:18118是否有效...\n",
      "检测223.245.176.158:61234是否有效...\n",
      "223.245.176.158:61234有效...\n",
      "检测125.120.201.2:6666是否有效...\n",
      "检测123.12.66.231:61234是否有效...\n",
      "检测183.159.80.63:18118是否有效...\n",
      "检测221.228.17.172:8181是否有效...\n",
      "221.228.17.172:8181有效...\n",
      "检测42.7.26.21:60443是否有效...\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import socket\n",
    "from bs4 import BeautifulSoup\n",
    "headers = {\n",
    "    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',\n",
    "    'Accept-Language': 'zh-CN,zh;q=0.8',\n",
    "    'Connection': 'keep-alive',\n",
    "    'Content-Type': 'application/x-www-form-urlencoded',\n",
    "    'User-Agent': \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 \"\n",
    "                  \"Safari/537.36 SE 2.X MetaSr 1.0\",\n",
    "}\n",
    "def get_ip():\n",
    "    proxy = []\n",
    "    for page in range(1, 2):\n",
    "        response = requests.get('http://www.xicidaili.com/nn/'+ str(page), headers=headers)\n",
    "        soup = BeautifulSoup(response.text, 'lxml')\n",
    "        ips = soup.find_all('tr')\n",
    "        for i in range(1, len(ips)):\n",
    "            ip_content = ips[i]\n",
    "            tdd = ip_content.find_all('td')\n",
    "            ip_port = tdd[1].get_text() + ':' + tdd[2].get_text()\n",
    "            proxy.append(ip_port)\n",
    "            print(ip_port)\n",
    "    return proxy\n",
    "def validate_ip(proxy):\n",
    "    ip = open('ip.txt', 'w')\n",
    "    url = \"http://ip.chinaz.com/getip.aspx\"\n",
    "    socket.setdefaulttimeout(3)\n",
    "    for i in range(0, len(proxy)):\n",
    "        try:\n",
    "            print('检测' + proxy[i] + '是否有效...')\n",
    "            proxy_host = \"http://\" + proxy[i]\n",
    "            proxy_temp = {\"http\": proxy_host}\n",
    "            res = requests.get(url, headers=headers, proxies=proxy_temp).text\n",
    "            print(proxy[i] + '有效...')\n",
    "            ip.write(proxy[i] + '\\n')\n",
    "        except Exception as e:\n",
    "            continue\n",
    "    ip.close()\n",
    "if __name__ == '__main__':\n",
    "    validate_ip(get_ip())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
